package com.ly.service.impl;

import com.ly.dao.SolutionMapper;
import com.ly.pojo.domain.SolutionDO;
import com.ly.pojo.dto.SolutionDTO;
import com.ly.pojo.query.SolutionQuery;
import com.ly.service.SolutionService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ly.exception.BusinessException;
import java.util.Map;
import com.ly.constant.ResultCodeConstant;
import java.util.Date;

/**
 * 解决方案管理模块的实现
 *
 * @author Administrator
 * @date 2025-03-07 10:51:13
 */
@Service
public class SolutionServiceImpl implements SolutionService {

    @Autowired
    private SolutionMapper solutionMapper;

    @Override
    public Boolean addSolution(SolutionDTO solutionDTO) {
        SolutionDO solutionDO = new SolutionDO();
        solutionDO.setTitle(solutionDTO.getTitle());
        solutionDO.setContent(solutionDTO.getContent());
        solutionDO.setImageUrl(solutionDTO.getImageUrl());
        // 示例创建人
        solutionDO.setCreateBy("admin");
        // 示例创建时间
        solutionDO.setCreateTime(new Date());
        int result = solutionMapper.insert(solutionDO);
        return result > 0;
    }

    @Override
    public Boolean updateSolution(SolutionDTO solutionDTO) {
        SolutionDO existingSolution = solutionMapper.selectById(solutionDTO.getSolutionId());
        if (existingSolution == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        existingSolution.setTitle(solutionDTO.getTitle());
        existingSolution.setContent(solutionDTO.getContent());
        existingSolution.setImageUrl(solutionDTO.getImageUrl());
        // 示例修改人
        existingSolution.setUpdateBy("admin");
        // 示例修改时间
        existingSolution.setUpdateTime(new Date());
        int result = solutionMapper.updateById(existingSolution);
        return result > 0;
    }

    @Override
    public Boolean deleteSolution(SolutionDTO solutionDTO) {
        SolutionDO existingSolution = solutionMapper.selectById(solutionDTO.getSolutionId());
        if (existingSolution == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        int result = solutionMapper.deleteById(solutionDTO.getSolutionId());
        return result > 0;
    }

    @Override
    public List<SolutionDO> getAllSolutions() {
        List<SolutionDO> solutions = solutionMapper.selectAll();
        return solutions;
    }

    @Override
    public SolutionDO getSolutionById(SolutionQuery solutionQuery) {
        SolutionDO solutionDO = solutionMapper.selectById(solutionQuery.getSolutionId());
        if (solutionDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        return solutionDO;
    }
}
